package com.qcnh.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qcnh.entity.User;
import com.qcnh.mapper.UserMapper;
import com.qcnh.service.impl.UserServiceImpl;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import unil.Result;
import unil.RedisConstains;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author ts
 * @since 2024-12-04
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    UserMapper userMapper;
    @Autowired
    UserServiceImpl userService;
    //管理员登录
    @RequestMapping("login")
    public Result login(@RequestParam(value = "phone") String phone,
                        @RequestParam(value = "password") String password ){
        String stoken=phone;
        Object token=redisTemplate.opsForValue().get("user:login：phone"+phone);
        //没有正在登录
        if (token!=null){
            return Result.fail(400,"已经登录",null);
        }
        //找到指定用户
        User user=new LambdaQueryChainWrapper<User>(userMapper)
                .eq(User::getPhone,phone)
                .eq(User::getPassword,password)
                .one();
        //用来存储token和用户信息传递给前端
        HashMap<Object,Object> map=new HashMap<>();
        if (user==null){
            return Result.fail(400,"没有该用户",null);
        }else {
            //将用户token存入reids中
            redisTemplate.opsForValue().set("user:login:phone"+phone,phone);

            map.put("token",token);
            map.put("user",user);
        }
        return  Result.ok(200,"登录成功",map);
    }

    //管理员退出登录
    @RequestMapping("logout")
    public Result loginout(@RequestParam(value = "phone") String phone){
        Boolean delete = redisTemplate.delete("user:login:phone"+phone);
        return delete?Result.ok(200,"退出登录成功",null)
                :Result.fail(400,"退出登录失败",null);
    }
    // 分页模糊查询所有用户
    @GetMapping("selectAll")
    public Result selectAll(@RequestParam(value = "current") Integer current,
                            @RequestParam(value = "size") Integer size,
                            @RequestParam(value = "keyWord") String keyWord) {
        Page<User> page = new Page<>(current,size);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(keyWord)) {
            queryWrapper.like("user_name", keyWord); // 假设根据用户名搜索
        }
        IPage<User> userIPage=userMapper.selectPage(page,queryWrapper);
        if (userIPage!=null){
            return Result.ok(200,"用户数据遍历成功",userIPage);
        }else {
            return
                    Result.fail(400,"用户数据遍历失败",null);
        }
    }
    // 根据id查询指定用户
    @GetMapping("selectById")
    public Result selectById(@RequestParam(value = "userId") Integer userId) {
        User user=new LambdaQueryChainWrapper<User>(userMapper)
                .eq(User::getUserId,userId)
                .one();
        if (user!=null){
            return Result.ok(200,"遍历该用户",user);
        }
        return Result.fail(400,"该用户不存在",null);
    }
    //添加单个用户
    @PostMapping("addUser")
    public Result addUser(@RequestBody User user) {
        int row=userMapper.insert(user);
        if (row>0){
            return Result.ok(200,"增加用户成功",null);
        }
        return Result.fail(400,"增加用户失败",null);
    }

    //根据id集合批量删除用户
    @DeleteMapping("delByIdList")
    public Result delByIdList(@RequestBody List<Integer> userIds){
        Boolean result=userService.removeByIds(userIds);
        if (result){
            return Result.ok(200,"删除用户成功",null);
        }
        System.out.println("ces");
        return  Result.fail(400,"删除用户失败",null);
    }
    //修改用户信息
    @PutMapping("updateMessage")
    public Result updateMessageById(@RequestBody User user){
        Boolean result=userService.updateById(user);
        if (result){
            return Result.ok(200,"修改用户成功",null);
        }
        return Result.fail(400,"修改用户失败",null);
    }
}

